﻿/*
' Copyright (c) 2013  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DNNCommunity.Modules.GroupMail.Components;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Exceptions;

namespace DNNCommunity.Modules.GroupMail
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from GroupMailModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : GroupMailModuleBase
    {

        #region Private Members

        private int p_GroupId = Null.NullInteger;
        private int p_GroupTabId = Null.NullInteger;
        private string p_GroupName = Null.NullString;

        #endregion

        #region Properties

        protected int GroupId
        {
            get
            {
                if (p_GroupId == Null.NullInteger)
                {
                    var oUserId = Request.QueryString["GroupId"];
                    if (oUserId != null) p_GroupId = int.Parse(oUserId.ToString(), System.Globalization.NumberStyles.Integer);
                }

                return p_GroupId;
            }
        }

        protected string GroupName
        {
            get
            {
                if (string.IsNullOrEmpty(p_GroupName))
                {
                    RoleController ctl = new RoleController();
                    RoleInfo role = ctl.GetRole(GroupId, PortalId);

                    p_GroupName = role.RoleName;
                }

                return p_GroupName;
            }
        }

        private int GroupTabId
        {
            get
            {
                if (p_GroupTabId == Null.NullInteger)
                {
                    var oTabId = Settings[FeatureController.SETTINGKEY_PROFILETABID];
                    if (oTabId != null) p_GroupTabId = int.Parse(oTabId.ToString(), System.Globalization.NumberStyles.Integer);
                }

                return p_GroupTabId;
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack) BindData();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region Helper Methods

        private void BindData()
        {
            LocalizeModule();

            // hide the module container (and the rest of the module as well)
            ContainerControl.Visible = (GroupId != Null.NullInteger && string.IsNullOrEmpty(GroupName) == false);
            
        }

        private void LocalizeModule()
        {
            // nothing to do here
        }

        #endregion

    }
}